//
//  HLRtcMessageCell.m
//  YueYue
//
//  Created by hulu001 on 2023/6/20.
//

#import "HLRtcMessageCell.h"
#import <FrameAccessor/FrameAccessor.h>
#import "HLUserOptionRequest.h"

@interface HLRtcMessageCell ()

@property (nonatomic, strong) UIImageView *avatarView;
@property (nonatomic, strong) UIView *containerView;
@property (nonatomic, strong) QMUILabel *contentLabel;// 内容
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) QMUILabel *transLabel;

@end

@implementation HLRtcMessageCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = UIColorClear;
        self.contentView.backgroundColor = UIColorClear;
        self.avatarView = [[UIImageView alloc] init];
        _avatarView.image = UIImageMake(@"rtc_message_tip");
        _avatarView.layer.masksToBounds = YES;
        _avatarView.layer.cornerRadius = 8;
        [self.contentView addSubview:_avatarView];
        [_avatarView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(0);
            make.size.mas_equalTo(26);
            make.top.mas_equalTo(6);
        }];
        
        self.containerView = [[UIView alloc] init];
        _containerView.layer.masksToBounds = YES;
        _containerView.layer.cornerRadius = 8;
        [self.contentView addSubview:_containerView];
        [_containerView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_avatarView);
            make.left.equalTo(_avatarView.mas_right).offset(4);
            make.right.mas_lessThanOrEqualTo(0);
            make.bottom.mas_equalTo(-6);
        }];
        
        self.contentLabel = [[QMUILabel alloc] qmui_initWithFont:UIFontMake(13) textColor:UIColorWhite];
        _contentLabel.numberOfLines = 0;
        _contentLabel.qmui_height = 18;
        [_containerView addSubview:_contentLabel];
        [_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(4);
            make.left.mas_equalTo(8);
            make.right.mas_equalTo(-8);
            make.bottom.mas_equalTo(-4);
        }];
        
        self.lineView = [[UIView alloc] init];
        _lineView.backgroundColor = [UIColorWhite colorWithAlphaComponent:0.15];
        [_containerView addSubview:_lineView];
        
        self.transLabel = [[QMUILabel alloc] qmui_initWithFont:UIFontMake(13) textColor:UIColorWhite];
        _transLabel.numberOfLines = 0;
        _transLabel.qmui_height = 18;
        [_containerView addSubview:_transLabel];
        _lineView.hidden = YES;
        _transLabel.hidden = YES;
        
    }
    return self;
}


- (void)setMessageModel:(HLRtcMessageModel *)messageModel
{
    _messageModel = messageModel;
    _lineView.hidden = YES;
    _transLabel.hidden = YES;
    if (messageModel.type == 1000){
        _avatarView.image = UIImageMake(@"rtc_message_tip");
        _containerView.backgroundColor = [[UIColor qmui_colorWithHexString:@"#646DD9"] colorWithAlphaComponent:0.4];
        _contentLabel.text = messageModel.ext;
        [_contentLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(4);
            make.left.mas_equalTo(8);
            make.right.mas_equalTo(-8);
            make.bottom.mas_equalTo(-4);
        }];
    } else {
        [_avatarView sd_setImageWithURL:[NSURL URLWithString:messageModel.fromAvatar] placeholderImage:[HLUIHelper avatarPlaceholder]];
        _contentLabel.text = [NSString stringWithFormat:@"%@: %@", messageModel.fromName, messageModel.text];
        if (![messageModel.from isEqualToString:HLRunTime.shareInstance.mine.userId]){
            _containerView.backgroundColor = [HLMainColor colorWithAlphaComponent:0.4];
            _lineView.hidden = NO;
            _transLabel.hidden = NO;
            NSString *expandString = messageModel.localExt[@"originContent"];
            _transLabel.text = expandString;
            [_contentLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(4);
                make.left.mas_equalTo(8);
                make.right.mas_equalTo(-8);
            }];
            [_lineView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.right.equalTo(_contentLabel);
                make.height.mas_equalTo(1);
                make.top.equalTo(_contentLabel.mas_bottom).offset(4);
            }];
            
            [_transLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.right.equalTo(_contentLabel);
                make.top.equalTo(_lineView.mas_bottom).offset(4);
                make.bottom.mas_equalTo(-4);
            }];
        } else {
            _containerView.backgroundColor = [UIColorBlack colorWithAlphaComponent:0.4];
            [_contentLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(4);
                make.left.mas_equalTo(8);
                make.right.mas_equalTo(-8);
                make.bottom.mas_equalTo(-4);
            }];
        }
    }
}

- (void)transMessage
{
    if (_messageModel.transText == nil){
        HLUserOptionRequest *userOptionRequest = [[HLUserOptionRequest alloc] initWithType:HLUserOptionTypeTrans params:@{@"body":_messageModel.ext}];
        [userOptionRequest startWithCompletionBlockWithSuccess:^(__kindof HLUserOptionRequest * _Nonnull request) {
            if (request.isSuccess){
                NSString *trans = request.result[@"data"];
                self.messageModel.transText = trans;
                [self.qmui_tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:self.messageModel.index inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
            }
        } failure:nil];
    }
}

@end
